﻿using System;
using System.Reactive.Concurrency;
using System.Reactive.Linq;
using System.Windows.Forms;

namespace Demo
{
	public partial class EnhancedLinqForm : Form
	{

		public EnhancedLinqForm()
		{
			InitializeComponent();
			GroupMouseMovesBySecond();
		}

		private void GroupMouseMovesBySecond()
		{
			//get all mouse moves
			var pts = from evt in Observable.FromEventPattern<MouseEventArgs>(this, "MouseMove")
					  select evt.EventArgs.Location;

			//collect mouse moves by second
			var ptsBySecond = (from buffer in pts.Buffer(TimeSpan.FromSeconds(2), new ControlScheduler(this))
			                   select buffer.Count);
			ptsBySecond.Subscribe(count => listBox1.Items.Insert(0,count.ToString()));



			//add 5 second delay before showing mouse moves
			var delayedList = (from item in ptsBySecond select item)
				.Delay(TimeSpan.FromSeconds(6), new ControlScheduler(this));
			delayedList.Subscribe(count => listBox2.Items.Insert(0, count.ToString()));

		}

        private void SubsetOfLinqOperators()
        {
            //standard methods
            var o = Observable.Range(1, 100);
            o.Any();
            o.FirstOrDefault();
            o.LastOrDefault();
            o.SingleOrDefault();
            o.Count();
            o.Min();
            o.Sum();
            o.Skip(4);

        }
	}




}